<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Grid;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\AdminUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Models\Administrator;
use Log;
use Dcat\Admin\Form;

class ProjectAuthController extends Controller
{
    public function index(Content $content)
    {
        // return $content->full()->body($this->grid());
        //超级管理员，可全部查看
        if (Admin::user()->isAdministrator()) {
            $model = new Project();
            return $content
                ->header('业主项目授权列表')
                // ->description('选择项目查看详情')
                ->body($this->grid($model));
        } else {
            return $content
            ->header('业主项目授权列表')
            ->description()
//            ->body(Callout::make('即将在下个版本发布，敬请期待~'))
            ->body($this->grid());
        }
    }

    protected function grid()
    {
        $ids = AdminUser::where(['pid'=>Admin::user()->id])->orWhere(['id'=>Admin::user()->id])->pluck('id');
        return Grid::make(AdminUser::whereIn('id',$ids), function (Grid $grid) {
            $grid->column('name','业主');
            $grid->column('id','授权项目')->display(function(){
                $project = ProjectUser::where('user_id',$this->id)->select('projectId')->get()->toArray();
                if($project){
                    $project = array_column($project,'projectId');
                    $projectlist = Project::whereIn('projectId',$project)->get()->toArray();
                    return implode('、',array_column($projectlist,'projectname'));
                }
            });
            // $model = Project::where('user_id',Admin::user()->id);
            // $grid->options()->checkbox([
            //     1 => 'Sed ut perspiciatis unde omni',
            //     2 => 'voluptatem accusantium doloremque',
            //     3 => 'dicta sunt explicabo',
            //     4 => 'laudantium, totam rem aperiam',
            // ]);
            // $grid->cloumn('projects','修改授权')->if(function () {
            //             return ! $this->roles->isEmpty();
            //         })->showTreeInDialog(function (Grid\Displayers\DialogTree $tree) use (&$nodes, $model) {
            //             Log::info($nodes);
            //             $tree->nodes($nodes);

            //             foreach (array_column($this->roles->toArray(), 'slug') as $slug) {
            //                 if ($roleModel::isAdministrator($slug)) {
            //                     $tree->checkAll();
            //                 }
            //             }
            //         })
            //         ->else()
            //         ->display('');
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->showColumnSelector();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            });

        // return Grid::make(ProjectUser::whereIn('user_id',$ids)->with(['projectId']), function (Grid $grid) {
        //     $grid->column('name','业主')
        //     ->display(function(){
        //         $user = Administrator::where(['id'=>$this->user_id])->select('name')->first();
        //         return $user->name;
        //     });
        //     $grid->column('projectId','授权项目')->pluck('projectname')->label('primary',3);
        // });
    }
    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    // public function create(Content $content){
    //     return $content
    //         ->title('新增项目')
    //         ->description('创建')
    //         // ->body($this->buildPreviewButton())
    //         // ->body($this->newline())
    //         ->body($this->form());
    // }
    public function form(){
        return Form::make(AdminUser::with(['projects']), function (Form $form) {
            $id = $form->getKey();
            // $name = AdminUser::where(['id'=>$id])->select('name')->get()->toArray();
            $form->display('id', '用户ID');
            $form->multipleSelect('projects','授权项目')->options(function(){
                    return Project::where(['user_id'=>Admin::user()->id])->pluck('projectname','projectId');
                })->customFormat(function ($value){
                    return array_column($value, 'projectId');
                });
        });
        // return Form::make(new ProjectUser(), function (Form $form) {
        //     $form->block(8, function (Form\BlockForm $form) {
        //         // 设置标题
        //         $form->title('基本设置');
        //         // 显示底部提交按钮
        //         $form->showFooter();
        //         // 设置字段宽度
        //         $form->width(9, 2);
        //         $form->column(6, function (Form\BlockForm $form) {
        //             $form->text('projectname','项目名称')->required();
        //             $form->text('remark','项目说明');
        //             $res = Company::where('user_id',Admin::user()->id)->pluck('name','companyId');
        //             $form->select('companyId', '所属公司')->options($res)->required();
        //             $form->decimal('lat','纬度');
        //             $form->decimal('lng','经度');
        //             $form->image('planeJPG','平面底图');
        //         });
        //     });
        // });
    }

    public function edit($id,Content $content){
        return $content
            ->title('项目编辑')
            ->description('编辑')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form()->edit($id));
    }

    public function show($id){
        return Show::make($id, new Project(), function (Show $show) {
            // $show->layerId;
            $show->field('projectname','项目名称');
            // $show->field('remark','公司备注');
            $show->created_at;
        });
    }
}
